home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / Utilities.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  15.7 KB  |  677 lines

  1. package horst;
  2.  
  3. import horst.parser.Tag;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Dimension;
  8. import java.awt.Font;
  9. import java.awt.FontMetrics;
  10. import java.awt.Frame;
  11. import java.awt.Graphics;
  12. import java.awt.GridBagConstraints;
  13. import java.awt.GridBagLayout;
  14. import java.awt.Image;
  15. import java.awt.Insets;
  16. import java.awt.MediaTracker;
  17. import java.awt.Polygon;
  18. import java.awt.Rectangle;
  19. import java.awt.Shape;
  20. import java.awt.Toolkit;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23. import java.util.Hashtable;
  24. import java.util.StringTokenizer;
  25. import java.util.Vector;
  26. import javax.swing.AbstractButton;
  27. import javax.swing.Icon;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JButton;
  30. import javax.swing.JCheckBox;
  31. import javax.swing.JComponent;
  32. import javax.swing.JRadioButton;
  33. import javax.swing.JTextArea;
  34. import javax.swing.JTextField;
  35. import javax.swing.border.EmptyBorder;
  36. import javax.swing.text.JTextComponent;
  37. import javax.swing.text.PlainDocument;
  38.  
  39. public class Utilities {
  40.    static boolean m_bDebugging;
  41.    static Color _brightColor = stringToColor("#dcdcdc");
  42.    static Color _darkColor;
  43.  
  44.    static {
  45.       _darkColor = _brightColor.darker().darker();
  46.    }
  47.  
  48.    public static final void addComponent(Container con, Component comp, int anchor, int fill, int gridheight, int gridwidth, int gridx, int gridy, Insets insets, int ipadx, int ipady, double weightx, double weighty) {
  49.       GridBagLayout gridbag = (GridBagLayout)con.getLayout();
  50.       GridBagConstraints c = new GridBagConstraints();
  51.       c.anchor = anchor;
  52.       c.fill = fill;
  53.       c.gridheight = gridheight;
  54.       c.gridwidth = gridwidth;
  55.       c.gridx = gridx;
  56.       c.gridy = gridy;
  57.       c.insets = insets;
  58.       c.ipadx = ipadx;
  59.       c.ipady = ipady;
  60.       c.weightx = weightx;
  61.       c.weighty = weighty;
  62.       gridbag.setConstraints(comp, c);
  63.       con.add(comp);
  64.    }
  65.  
  66.    public static final int[] clone(int[] ary) {
  67.       int[] copy = new int[ary.length];
  68.  
  69.       for(int i = 0; i < copy.length; ++i) {
  70.          copy[i] = ary[i];
  71.       }
  72.  
  73.       return copy;
  74.    }
  75.  
  76.    static final Component createFormComponent(HTMLDocument doc, Tag t) {
  77.       Component c = null;
  78.       if (t.getID() == 30) {
  79.          String type = (String)t.getAttribute("type");
  80.          if (type == null) {
  81.             c = new TextInput();
  82.             setFieldAttributes(t, (TextInput)c);
  83.             return c;
  84.          }
  85.  
  86.          if (!type.equalsIgnoreCase("submit") && !type.equalsIgnoreCase("reset") && !type.equalsIgnoreCase("button")) {
  87.             if (type.equalsIgnoreCase("image")) {
  88.                String src = (String)t.getAttribute("src");
  89.  
  90.                JButton button;
  91.                try {
  92.                   URL base = doc.getBaseURL();
  93.                   URL srcURL = new URL(base, src);
  94.                   Icon icon = new ImageIcon(srcURL);
  95.                   button = new JButton(icon);
  96.                   ((JComponent)button).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0)));
  97.                   ((AbstractButton)button).setMargin(new Insets(0, 0, 0, 0));
  98.                   ((JComponent)button).setOpaque(false);
  99.                   ((AbstractButton)button).setBorderPainted(false);
  100.                } catch (MalformedURLException var9) {
  101.                   button = new JButton();
  102.                }
  103.  
  104.                c = button;
  105.             } else if (type.equalsIgnoreCase("checkbox")) {
  106.                c = new JCheckBox();
  107.                if (t.isAttributeDefined("checked")) {
  108.                   ((JCheckBox)c).setSelected(true);
  109.                }
  110.  
  111.                ((JCheckBox)c).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0)));
  112.                ((JCheckBox)c).setOpaque(false);
  113.             } else if (type.equalsIgnoreCase("radio")) {
  114.                c = new JRadioButton();
  115.                if (t.isAttributeDefined("checked")) {
  116.                   ((JRadioButton)c).setSelected(true);
  117.                }
  118.  
  119.                ((JRadioButton)c).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0)));
  120.                ((JRadioButton)c).setOpaque(false);
  121.             } else if (type.equalsIgnoreCase("text")) {
  122.                c = new TextInput();
  123.                setFieldAttributes(t, (TextInput)c);
  124.             } else if (type.equalsIgnoreCase("password")) {
  125.                c = new TextInput();
  126.                setFieldAttributes(t, (TextInput)c);
  127.             }
  128.          } else {
  129.             String value = (String)t.getAttribute("value");
  130.             if (value == null) {
  131.                if (type.equalsIgnoreCase("submit")) {
  132.                   value = "submit";
  133.                } else {
  134.                   value = "reset";
  135.                }
  136.             }
  137.  
  138.             JButton button = new JButton(value);
  139.             Dimension prefSize = ((JComponent)button).getPreferredSize();
  140.             prefSize.height = getButtonHeight();
  141.             ((JComponent)button).setPreferredSize(prefSize);
  142.             ((JComponent)button).setOpaque(true);
  143.             c = button;
  144.          }
  145.       } else if (t.getID() == 32) {
  146.          int columns = 10;
  147.          int rows = 10;
  148.          String colStr = (String)t.getAttribute("cols");
  149.          if (colStr != null) {
  150.             Integer val = getInteger(colStr);
  151.             if (val != null && val > 0) {
  152.                columns = val;
  153.             }
  154.          }
  155.  
  156.          String rowStr = (String)t.getAttribute("rows");
  157.          if (rowStr != null) {
  158.             Integer val = getInteger(rowStr);
  159.             if (val != null && val > 0) {
  160.                rows = val;
  161.             }
  162.          }
  163.  
  164.          PlainDocument d = new PlainDocument();
  165.          JTextArea ta = new JTextArea(d, "", rows, columns);
  166.          ta.setLineWrap(true);
  167.          c = ta;
  168.       } else {
  169.          c = new TextInput();
  170.          setFieldAttributes(t, (TextInput)c);
  171.       }
  172.  
  173.       return c;
  174.    }
  175.  
  176.    public static final Shape createShape(String name, String coordinates) {
  177.       Shape s = null;
  178.       StringTokenizer st = new StringTokenizer(coordinates, ", ", false);
  179.       if (name != null && name.equalsIgnoreCase("rect") || name == null && st.countTokens() == 4) {
  180.          Rectangle r = new Rectangle();
  181.          s = r;
  182.          int nCount = 0;
  183.  
  184.          while(st.hasMoreTokens()) {
  185.             Integer iVal = getInteger(st.nextToken());
  186.             if (iVal != null) {
  187.                switch (nCount) {
  188.                   case 0:
  189.                      r.x = iVal;
  190.                      ++nCount;
  191.                      break;
  192.                   case 1:
  193.                      r.y = iVal;
  194.                      ++nCount;
  195.                      break;
  196.                   case 2:
  197.                      r.width = iVal - r.x;
  198.                      ++nCount;
  199.                      break;
  200.                   case 3:
  201.                      r.height = iVal - r.y;
  202.                      ++nCount;
  203.                }
  204.             }
  205.          }
  206.       } else {
  207.          Vector xVector = new Vector();
  208.          Vector yVector = new Vector();
  209.          boolean bIsXVal = true;
  210.  
  211.          while(st.hasMoreTokens()) {
  212.             Integer iVal = getInteger(st.nextToken());
  213.             if (iVal != null) {
  214.                if (bIsXVal) {
  215.                   xVector.addElement(iVal);
  216.                } else {
  217.                   yVector.addElement(iVal);
  218.                }
  219.  
  220.                bIsXVal = !bIsXVal;
  221.             }
  222.          }
  223.  
  224.          int nPoints = Math.min(xVector.size(), yVector.size());
  225.          int[] xPoints = new int[nPoints];
  226.          int[] yPoints = new int[nPoints];
  227.  
  228.          for(int i = 0; i < xPoints.length; ++i) {
  229.             xPoints[i] = (Integer)xVector.elementAt(i);
  230.          }
  231.  
  232.          for(int i = 0; i < yPoints.length; ++i) {
  233.             yPoints[i] = (Integer)yVector.elementAt(i);
  234.          }
  235.  
  236.          s = new Polygon(xPoints, yPoints, nPoints);
  237.       }
  238.  
  239.       return s;
  240.    }
  241.  
  242.    static final void debugOut(String msg) {
  243.       if (m_bDebugging) {
  244.          System.out.println(msg);
  245.       }
  246.  
  247.    }
  248.  
  249.    static final void drawBorder(Graphics g, Rectangle r) {
  250.       g.setColor(_brightColor);
  251.       g.drawLine(r.x, r.y + r.height - 1, r.x + r.width - 1, r.y + r.height - 1);
  252.       g.drawLine(r.x + r.width - 1, r.y, r.x + r.width - 1, r.y + r.height - 1);
  253.       g.setColor(_darkColor);
  254.       g.drawLine(r.x, r.y, r.x + r.width - 1, r.y);
  255.       g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
  256.    }
  257.  
  258.    public static final void drawDottedLine(Graphics g, int x1, int y1, int x2, int y2) {
  259.       if (x1 == x2) {
  260.          if (y1 > y2) {
  261.             int temp = y2;
  262.             y2 = y1;
  263.             y1 = temp;
  264.          }
  265.  
  266.          if ((x1 & 1 ^ y1 & 1) != 0) {
  267.             ++y1;
  268.          }
  269.  
  270.          while(y1 < y2) {
  271.             g.drawLine(x1, y1, x1, y1);
  272.             y1 += 2;
  273.          }
  274.  
  275.       } else if (y1 != y2) {
  276.          throw new IllegalArgumentException("Only horizonatl and Vertical Lines");
  277.       } else {
  278.          if (y1 > y2) {
  279.             y1 = y2;
  280.          }
  281.  
  282.          if ((x1 & 1 ^ y1 & 1) != 0) {
  283.             ++x1;
  284.          }
  285.  
  286.          while(x1 < x2) {
  287.             g.drawLine(x1, y1, x1, y1);
  288.             x1 += 2;
  289.          }
  290.  
  291.       }
  292.    }
  293.  
  294.    public static final void drawDottedRectangle(Graphics g, int x, int y, int w, int h) {
  295.       int x2 = x + w;
  296.       int y2 = y + h;
  297.       drawDottedLine(g, x, y, x2, y);
  298.       drawDottedLine(g, x2, y, x2, y2);
  299.       drawDottedLine(g, x, y2, x2, y2);
  300.       drawDottedLine(g, x, y, x, y2);
  301.    }
  302.  
  303.    public static final void drawFocusBorder(Graphics g, Rectangle r) {
  304.       g.setColor(Color.orange);
  305.  
  306.       for(int i = 0; i < 2; ++i) {
  307.          g.drawRect(r.x + i, r.y + i, r.width - 2 * i, r.height - 2 * i);
  308.       }
  309.  
  310.    }
  311.  
  312.    static final Color getBrightBorderColor() {
  313.       return _brightColor;
  314.    }
  315.  
  316.    static final int getButtonHeight() {
  317.       return 25;
  318.    }
  319.  
  320.    static final Color getDarkBorderColor() {
  321.       return _darkColor;
  322.    }
  323.  
  324.    public static final Frame getFrame(Component c) {
  325.       if (c instanceof Frame) {
  326.          return (Frame)c;
  327.       } else {
  328.          Frame frame = null;
  329.  
  330.          Component parent;
  331.          for(parent = c.getParent(); parent != null && !(parent instanceof Frame); parent = parent.getParent()) {
  332.          }
  333.  
  334.          if (parent != null) {
  335.             frame = (Frame)parent;
  336.          } else {
  337.             frame = new Frame();
  338.          }
  339.  
  340.          return frame;
  341.       }
  342.    }
  343.  
  344.    static final Integer getInteger(String val) {
  345.       Integer i = null;
  346.  
  347.       try {
  348.          i = new Integer(val);
  349.       } catch (NumberFormatException var2) {
  350.       }
  351.  
  352.       return i;
  353.    }
  354.  
  355.    public static final String getInternetFormat(String str) {
  356.       String s = "";
  357.  
  358.       for(int i = 0; i < str.length(); ++i) {
  359.          char c = str.charAt(i);
  360.          if (c == '\\') {
  361.             s = s + "/";
  362.          } else {
  363.             s = s + c;
  364.          }
  365.       }
  366.  
  367.       return s;
  368.    }
  369.  
  370.    static final URL getURL(String src) {
  371.       URL u = null;
  372.       src = removeNewlines(src);
  373.  
  374.       try {
  375.          u = new URL(src);
  376.       } catch (MalformedURLException var2) {
  377.       }
  378.  
  379.       return u;
  380.    }
  381.  
  382.    static final URL getURL(URL baseURL, String src) {
  383.       URL u = null;
  384.       src = removeNewlines(src);
  385.  
  386.       try {
  387.          u = new URL(baseURL, src);
  388.       } catch (MalformedURLException var3) {
  389.       }
  390.  
  391.       return u;
  392.    }
  393.  
  394.    static final Color hexToColor(String value) {
  395.       if (value.startsWith("#")) {
  396.          String digits = value.substring(1, Math.min(value.length(), 7));
  397.          String hstr = "0x" + digits;
  398.  
  399.          try {
  400.             Color c = Color.decode(hstr);
  401.             return c;
  402.          } catch (NumberFormatException var4) {
  403.          }
  404.       }
  405.  
  406.       return null;
  407.    }
  408.  
  409.    public static final boolean isBlank(char[] data) {
  410.       int len = data.length;
  411.  
  412.       for(int i = 0; i < len; ++i) {
  413.          if (!Character.isWhitespace(data[i])) {
  414.             return false;
  415.          }
  416.       }
  417.  
  418.       return true;
  419.    }
  420.  
  421.    static final boolean isBlankSpaces(String txt) {
  422.       if (txt != null && txt.length() != 0) {
  423.          for(int i = 0; i < txt.length(); ++i) {
  424.             if (txt.charAt(i) != ' ' && txt.charAt(i) != '\n') {
  425.                return false;
  426.             }
  427.          }
  428.  
  429.          return true;
  430.       } else {
  431.          return true;
  432.       }
  433.    }
  434.  
  435.    static final Image loadImage(Component c, String path) {
  436.       Image i = Toolkit.getDefaultToolkit().getImage(path);
  437.       if (i == null) {
  438.          return i;
  439.       } else {
  440.          MediaTracker tracker = new MediaTracker(c);
  441.          tracker.addImage(i, 0);
  442.  
  443.          try {
  444.             tracker.waitForID(0);
  445.             if (tracker.isErrorAny()) {
  446.                return null;
  447.             }
  448.          } catch (InterruptedException var4) {
  449.          }
  450.  
  451.          return i;
  452.       }
  453.    }
  454.  
  455.    static final Integer parseInteger(String s, char c) {
  456.       Integer i = null;
  457.       int index;
  458.       if ((index = s.indexOf(c)) != -1) {
  459.          try {
  460.             s = s.substring(0, index).trim();
  461.             i = new Integer(s);
  462.          } catch (NumberFormatException var4) {
  463.          }
  464.       }
  465.  
  466.       return i;
  467.    }
  468.  
  469.    static String removeNewlines(String s) {
  470.       StringBuffer buf = new StringBuffer();
  471.  
  472.       for(int i = 0; i < s.length(); ++i) {
  473.          if (s.charAt(i) != '\n') {
  474.             buf.append(s.charAt(i));
  475.          }
  476.       }
  477.  
  478.       return buf.toString();
  479.    }
  480.  
  481.    static final String removeTrailingBackslashes(String text) {
  482.       while(text.endsWith("/")) {
  483.          text = text.substring(0, text.length() - 1);
  484.       }
  485.  
  486.       return text;
  487.    }
  488.  
  489.    static final int setAlignmentProperty(boolean bVertical, int defaultAlignment, Object key, Hashtable hash) {
  490.       String val = (String)hash.get(key);
  491.       if (val != null) {
  492.          if (bVertical) {
  493.             if (val.toLowerCase().equals("top")) {
  494.                return 0;
  495.             }
  496.  
  497.             if (val.toLowerCase().equals("bottom")) {
  498.                return 2;
  499.             }
  500.          } else {
  501.             if (val.toLowerCase().equals("right")) {
  502.                return 2;
  503.             }
  504.  
  505.             if (val.toLowerCase().equals("left")) {
  506.                return 0;
  507.             }
  508.  
  509.             if (val.toLowerCase().equals("center") || val.toLowerCase().equals("middle")) {
  510.                return 1;
  511.             }
  512.          }
  513.       }
  514.  
  515.       return defaultAlignment;
  516.    }
  517.  
  518.    static final boolean setBooleanProperty(boolean defaultVal, Object key, Hashtable hash) {
  519.       String src = (String)hash.get(key);
  520.       if (src != null) {
  521.          if (src.equalsIgnoreCase("yes") || src.equalsIgnoreCase("true") || src.equalsIgnoreCase("1")) {
  522.             return true;
  523.          }
  524.  
  525.          if (src.equalsIgnoreCase("no") || src.equalsIgnoreCase("false") || src.equalsIgnoreCase("0")) {
  526.             return false;
  527.          }
  528.       }
  529.  
  530.       return defaultVal;
  531.    }
  532.  
  533.    static final Color setColorProperty(Color defaultColor, Object key, Hashtable hash) {
  534.       Object val = hash.get(key);
  535.       if (val == null) {
  536.          return defaultColor;
  537.       } else if (val instanceof Color) {
  538.          return (Color)val;
  539.       } else {
  540.          String str = (String)val;
  541.          Color c = null;
  542.          if (str != null) {
  543.             c = stringToColor(str);
  544.          }
  545.  
  546.          return c != null ? c : defaultColor;
  547.       }
  548.    }
  549.  
  550.    private static final void setFieldAttributes(Tag t, TextInput field) {
  551.       ((JTextField)field).setFont(new Font("Courier", 0, 12));
  552.       ((JTextComponent)field).setOpaque(true);
  553.       int size = 20;
  554.       String len = (String)t.getAttribute("maxlength");
  555.       if (len != null) {
  556.          Integer iVal = getInteger(len);
  557.          if (iVal != null && iVal > 0) {
  558.             size = iVal;
  559.          }
  560.       }
  561.  
  562.       ((JTextField)field).setColumns(size);
  563.       String sz = (String)t.getAttribute("size");
  564.       if (sz != null) {
  565.          Integer iVal = getInteger(sz);
  566.          if (iVal != null && iVal > 0) {
  567.             int h = getButtonHeight();
  568.             FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(((Component)field).getFont());
  569.             int w = iVal * field.getColumnWidth();
  570.             field.setPreferredSize(new Dimension(w, h));
  571.          }
  572.       }
  573.  
  574.       String value = (String)t.getAttribute("value");
  575.       if (value != null) {
  576.          ((JTextComponent)field).setText(value);
  577.       }
  578.  
  579.    }
  580.  
  581.    static final int setIntegerProperty(int defaultVal, Object key, Hashtable hash) {
  582.       String val = (String)hash.get(key);
  583.       if (val != null && val.length() > 0) {
  584.          Integer iVal = getInteger(val);
  585.          if (iVal != null && iVal >= 0) {
  586.             return iVal;
  587.          }
  588.       }
  589.  
  590.       return defaultVal;
  591.    }
  592.  
  593.    static final float setPercentageProperty(float defaultVal, Object key, Hashtable hash) {
  594.       String src = (String)hash.get(key);
  595.       if (src != null) {
  596.          int idx = src.indexOf(37);
  597.          if (idx != -1) {
  598.             Integer val = getInteger(src.substring(0, idx));
  599.             if (val != null && val > 0 && val <= 100) {
  600.                return (float)val / 100.0F;
  601.             }
  602.          }
  603.       }
  604.  
  605.       return defaultVal;
  606.    }
  607.  
  608.    static final URL setURLProperty(URL baseURL, Object key, Hashtable hash) {
  609.       URL u = null;
  610.       String src = (String)hash.get(key);
  611.       if (src != null && src.length() > 0) {
  612.          if (baseURL != null) {
  613.             u = getURL(baseURL, src);
  614.          } else {
  615.             u = getURL(src);
  616.          }
  617.       }
  618.  
  619.       return u;
  620.    }
  621.  
  622.    static final Color stringToColor(String str) {
  623.       if (str == null | str.length() == 0) {
  624.          return null;
  625.       } else {
  626.          Color color = null;
  627.          if (str.charAt(0) == '#') {
  628.             color = hexToColor(str);
  629.          } else if (str.equals("orange")) {
  630.             color = Color.orange;
  631.          } else if (str.equals("magenta")) {
  632.             color = Color.magenta;
  633.          } else if (str.equals("darkmagenta")) {
  634.             color = Color.magenta.darker();
  635.          } else if (str.equalsIgnoreCase("gold")) {
  636.             color = Color.orange;
  637.          } else if (str.equalsIgnoreCase("Black")) {
  638.             color = hexToColor("#000000");
  639.          } else if (str.equalsIgnoreCase("Silver")) {
  640.             color = hexToColor("#C0C0C0");
  641.          } else if (str.equalsIgnoreCase("Gray")) {
  642.             color = hexToColor("#808080");
  643.          } else if (str.equalsIgnoreCase("White")) {
  644.             color = hexToColor("#FFFFFF");
  645.          } else if (str.equalsIgnoreCase("Maroon")) {
  646.             color = hexToColor("#800000");
  647.          } else if (str.equalsIgnoreCase("Red")) {
  648.             color = hexToColor("#FF0000");
  649.          } else if (str.equalsIgnoreCase("Purple")) {
  650.             color = hexToColor("#800080");
  651.          } else if (str.equalsIgnoreCase("Fuchsia")) {
  652.             color = hexToColor("#FF00FF");
  653.          } else if (str.equalsIgnoreCase("Green")) {
  654.             color = hexToColor("#008000");
  655.          } else if (str.equalsIgnoreCase("Lime")) {
  656.             color = hexToColor("#00FF00");
  657.          } else if (str.equalsIgnoreCase("Olive")) {
  658.             color = hexToColor("#808000");
  659.          } else if (str.equalsIgnoreCase("Yellow")) {
  660.             color = hexToColor("#FFFF00");
  661.          } else if (str.equalsIgnoreCase("Navy")) {
  662.             color = hexToColor("#000080");
  663.          } else if (str.equalsIgnoreCase("Blue")) {
  664.             color = hexToColor("#0000FF");
  665.          } else if (str.equalsIgnoreCase("Teal")) {
  666.             color = hexToColor("#008080");
  667.          } else if (str.equalsIgnoreCase("Aqua")) {
  668.             color = hexToColor("#00FFFF");
  669.          } else {
  670.             color = hexToColor("#" + str);
  671.          }
  672.  
  673.          return color;
  674.       }
  675.    }
  676. }
  677.